<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-US">
<head>
<!-- GenHTML revision 25226-->
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>The cart Example - The Java EE 6 Tutorial</title>
<meta name="robots" content="index,follow">
<meta name="robots" content="index,follow">
<meta name="date" content="2011-03-01">
<link rel="stylesheet" type="text/css" href="css/default.css">
<link rel="stylesheet" type="text/css" href="css/ipg.css">
<link rel="stylesheet" type="text/css" href="css/javaeetutorial.css">
</head>

<body>

<table border="0" cellpadding="5" cellspacing="0" width="100%">
<tbody>
   <tr valign="top">
      <td width="400px"><p class="toc level1"><a href="docinfo.html">Document Information</a></p>
<p class="toc level1 tocsp"><a href="gexaf.html">Preface</a></p>
<p class="toc level1 tocsp"><a href="gfirp.html">Part&nbsp;I&nbsp;Introduction</a></p>
<p class="toc level2"><a href="bnaaw.html">1.&nbsp;&nbsp;Overview</a></p>
<p class="toc level2"><a href="gfiud.html">2.&nbsp;&nbsp;Using the Tutorial Examples</a></p>
<p class="toc level1 tocsp"><a href="bnadp.html">Part&nbsp;II&nbsp;The Web Tier</a></p>
<p class="toc level2"><a href="bnadr.html">3.&nbsp;&nbsp;Getting Started with Web Applications</a></p>
<p class="toc level2"><a href="bnaph.html">4.&nbsp;&nbsp;JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="giepx.html">5.&nbsp;&nbsp;Introduction to Facelets</a></p>
<p class="toc level2"><a href="gjddd.html">6.&nbsp;&nbsp;Expression Language</a></p>
<p class="toc level2"><a href="bnaqz.html">7.&nbsp;&nbsp;Using JavaServer Faces Technology in Web Pages</a></p>
<p class="toc level2"><a href="gjcut.html">8.&nbsp;&nbsp;Using Converters, Listeners, and Validators</a></p>
<p class="toc level2"><a href="bnatx.html">9.&nbsp;&nbsp;Developing with JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="gkmaa.html">10.&nbsp;&nbsp;JavaServer Faces Technology Advanced Concepts</a></p>
<p class="toc level2"><a href="bnawo.html">11.&nbsp;&nbsp;Configuring JavaServer Faces Applications</a></p>
<p class="toc level2"><a href="gkiow.html">12.&nbsp;&nbsp;Using Ajax with JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="gkhxa.html">13.&nbsp;&nbsp;Advanced Composite Components</a></p>
<p class="toc level2"><a href="bnavg.html">14.&nbsp;&nbsp;Creating Custom UI Components</a></p>
<p class="toc level2"><a href="bnafd.html">15.&nbsp;&nbsp;Java Servlet Technology</a></p>
<p class="toc level2"><a href="bnaxu.html">16.&nbsp;&nbsp;Internationalizing and Localizing Web Applications</a></p>
<p class="toc level1 tocsp"><a href="bnayk.html">Part&nbsp;III&nbsp;Web Services</a></p>
<p class="toc level2"><a href="gijti.html">17.&nbsp;&nbsp;Introduction to Web Services</a></p>
<p class="toc level2"><a href="bnayl.html">18.&nbsp;&nbsp;Building Web Services with JAX-WS</a></p>
<p class="toc level2"><a href="giepu.html">19.&nbsp;&nbsp;Building RESTful Web Services with JAX-RS</a></p>
<p class="toc level2"><a href="gjjxe.html">20.&nbsp;&nbsp;Advanced JAX-RS Features</a></p>
<p class="toc level2"><a href="gkojl.html">21.&nbsp;&nbsp;Running the Advanced JAX-RS Example Application</a></p>
<p class="toc level1 tocsp"><a href="bnblr.html">Part&nbsp;IV&nbsp;Enterprise Beans</a></p>
<p class="toc level2"><a href="gijsz.html">22.&nbsp;&nbsp;Enterprise Beans</a></p>
<p class="toc level2"><a href="gijre.html">23.&nbsp;&nbsp;Getting Started with Enterprise Beans</a></p>
<p class="toc level2"><a href="gijrb.html">24.&nbsp;&nbsp;Running the Enterprise Bean Examples</a></p>
<div id="scrolltoc" class="onpage">
<p class="toc level3"><a href="">The <tt>cart</tt> Example</a></p>
<p class="toc level4"><a href="#bnboe">The Business Interface</a></p>
<p class="toc level4"><a href="#bnbof">Session Bean Class</a></p>
<p class="toc level5"><a href="#bnbog">Lifecycle Callback Methods</a></p>
<p class="toc level5"><a href="#bnboh">Business Methods</a></p>
<p class="toc level4 tocsp"><a href="#bnboi">The <tt>@Remove</tt> Method</a></p>
<p class="toc level4"><a href="#bnboj">Helper Classes</a></p>
<p class="toc level4"><a href="#bnbok">Building, Packaging, Deploying, and Running the <tt>cart</tt> Example</a></p>
<p class="toc level5"><a href="#bnbol">To Build, Package, Deploy, and Run the <tt>cart</tt> Example Using NetBeans IDE</a></p>
<p class="toc level5"><a href="#bnbon">To Build, Package, Deploy, and Run the <tt>cart</tt> Example Using Ant</a></p>
<p class="toc level5"><a href="#bnbop">The <tt>all</tt> Task</a></p>
</div>
<p class="toc level3 tocsp"><a href="gipvi.html">A Singleton Session Bean Example: <tt>counter</tt></a></p>
<p class="toc level4"><a href="gipvi.html#gipvc">Creating a Singleton Session Bean</a></p>
<p class="toc level5"><a href="gipvi.html#gippq">Initializing Singleton Session Beans</a></p>
<p class="toc level5"><a href="gipvi.html#gipsz">Managing Concurrent Access in a Singleton Session Bean</a></p>
<p class="toc level5"><a href="gipvi.html#gipvd">Handling Errors in a Singleton Session Bean</a></p>
<p class="toc level4 tocsp"><a href="gipvi.html#gipxl">The Architecture of the <tt>counter</tt> Example</a></p>
<p class="toc level4"><a href="gipvi.html#gipvl">Building, Packaging, Deploying, and Running the <tt>counter</tt> Example</a></p>
<p class="toc level5"><a href="gipvi.html#gipxt">To Build, Package, Deploy, and Run the <tt>counter</tt> Example Using NetBeans IDE</a></p>
<p class="toc level5"><a href="gipvi.html#gipzw">To Build, Package, Deploy, and Run the <tt>counter</tt> Example Using Ant</a></p>
<p class="toc level3 tocsp"><a href="bnbor.html">A Web Service Example: <tt>helloservice</tt></a></p>
<p class="toc level4"><a href="bnbor.html#bnbos">The Web Service Endpoint Implementation Class</a></p>
<p class="toc level4"><a href="bnbor.html#bnbot">Stateless Session Bean Implementation Class</a></p>
<p class="toc level4"><a href="bnbor.html#bnbou">Building, Packaging, Deploying, and Testing the <tt>helloservice</tt> Example</a></p>
<p class="toc level5"><a href="bnbor.html#bnbov">To Build, Package, and Deploy the <tt>helloservice</tt> Example Using NetBeans IDE</a></p>
<p class="toc level5"><a href="bnbor.html#bnbow">To Build, Package, and Deploy the <tt>helloservice</tt> Example Using Ant</a></p>
<p class="toc level5"><a href="bnbor.html#bnbox">To Test the Service without a Client</a></p>
<p class="toc level3 tocsp"><a href="bnboy.html">Using the Timer Service</a></p>
<p class="toc level4"><a href="bnboy.html#giqlk">Creating Calendar-Based Timer Expressions</a></p>
<p class="toc level5"><a href="bnboy.html#giqmx">Specifying Multiple Values in Calendar Expressions</a></p>
<p class="toc level4 tocsp"><a href="bnboy.html#giqlt">Programmatic Timers</a></p>
<p class="toc level5"><a href="bnboy.html#bnboz">The <tt>@Timeout</tt> Method</a></p>
<p class="toc level5"><a href="bnboy.html#bnbpa">Creating Programmatic Timers</a></p>
<p class="toc level4 tocsp"><a href="bnboy.html#giqmb">Automatic Timers</a></p>
<p class="toc level4"><a href="bnboy.html#bnbpb">Canceling and Saving Timers</a></p>
<p class="toc level4"><a href="bnboy.html#bnbpc">Getting Timer Information</a></p>
<p class="toc level4"><a href="bnboy.html#bnbpd">Transactions and Timers</a></p>
<p class="toc level4"><a href="bnboy.html#bnbpe">The <tt>timersession</tt> Example</a></p>
<p class="toc level4"><a href="bnboy.html#bnbpf">Building, Packaging, Deploying, and Running the <tt>timersession</tt> Example</a></p>
<p class="toc level5"><a href="bnboy.html#giqni">To Build, Package, Deploy, and Run the <tt>timersession</tt> Example Using NetBeans IDE</a></p>
<p class="toc level5"><a href="bnboy.html#giqnq">To Build, Package, and Deploy the <tt>timersession</tt> Example Using Ant</a></p>
<p class="toc level5"><a href="bnboy.html#giqop">To Run the Web Client</a></p>
<p class="toc level3 tocsp"><a href="bnbpj.html">Handling Exceptions</a></p>
<p class="toc level2 tocsp"><a href="bnbpk.html">25.&nbsp;&nbsp;A Message-Driven Bean Example</a></p>
<p class="toc level2"><a href="gkcqz.html">26.&nbsp;&nbsp;Using the Embedded Enterprise Bean Container</a></p>
<p class="toc level2"><a href="gkidz.html">27.&nbsp;&nbsp;Using Asynchronous Method Invocation in Session Beans</a></p>
<p class="toc level1 tocsp"><a href="gjbnr.html">Part&nbsp;V&nbsp;Contexts and Dependency Injection for the Java EE Platform</a></p>
<p class="toc level2"><a href="giwhb.html">28.&nbsp;&nbsp;Introduction to Contexts and Dependency Injection for the Java EE Platform</a></p>
<p class="toc level2"><a href="gjbls.html">29.&nbsp;&nbsp;Running the Basic Contexts and Dependency Injection Examples</a></p>
<p class="toc level2"><a href="gjehi.html">30.&nbsp;&nbsp;Contexts and Dependency Injection for the Java EE Platform: Advanced Topics</a></p>
<p class="toc level2"><a href="gkhre.html">31.&nbsp;&nbsp;Running the Advanced Contexts and Dependency Injection Examples</a></p>
<p class="toc level1 tocsp"><a href="bnbpy.html">Part&nbsp;VI&nbsp;Persistence</a></p>
<p class="toc level2"><a href="bnbpz.html">32.&nbsp;&nbsp;Introduction to the Java Persistence API</a></p>
<p class="toc level2"><a href="gijst.html">33.&nbsp;&nbsp;Running the Persistence Examples</a></p>
<p class="toc level2"><a href="bnbtg.html">34.&nbsp;&nbsp;The Java Persistence Query Language</a></p>
<p class="toc level2"><a href="gjitv.html">35.&nbsp;&nbsp;Using the Criteria API to Create Queries</a></p>
<p class="toc level2"><a href="gkjiq.html">36.&nbsp;&nbsp;Creating and Using String-Based Criteria Queries</a></p>
<p class="toc level2"><a href="gkjjf.html">37.&nbsp;&nbsp;Controlling Concurrent Access to Entity Data with Locking</a></p>
<p class="toc level2"><a href="gkjia.html">38.&nbsp;&nbsp;Improving the Performance of Java Persistence API Applications By Setting a Second-Level Cache</a></p>
<p class="toc level1 tocsp"><a href="gijrp.html">Part&nbsp;VII&nbsp;Security</a></p>
<p class="toc level2"><a href="bnbwj.html">39.&nbsp;&nbsp;Introduction to Security in the Java EE Platform</a></p>
<p class="toc level2"><a href="bncas.html">40.&nbsp;&nbsp;Getting Started Securing Web Applications</a></p>
<p class="toc level2"><a href="bnbyk.html">41.&nbsp;&nbsp;Getting Started Securing Enterprise Applications</a></p>
<p class="toc level1 tocsp"><a href="gijue.html">Part&nbsp;VIII&nbsp;Java EE Supporting Technologies</a></p>
<p class="toc level2"><a href="gijto.html">42.&nbsp;&nbsp;Introduction to Java EE Supporting Technologies</a></p>
<p class="toc level2"><a href="bncih.html">43.&nbsp;&nbsp;Transactions</a></p>
<p class="toc level2"><a href="bncjh.html">44.&nbsp;&nbsp;Resource Connections</a></p>
<p class="toc level2"><a href="bncdq.html">45.&nbsp;&nbsp;Java Message Service Concepts</a></p>
<p class="toc level2"><a href="bncgv.html">46.&nbsp;&nbsp;Java Message Service Examples</a></p>
<p class="toc level2"><a href="gkahp.html">47.&nbsp;&nbsp;Advanced Bean Validation Concepts and Examples</a></p>
<p class="toc level2"><a href="gkeed.html">48.&nbsp;&nbsp;Using Java EE Interceptors</a></p>
<p class="toc level1 tocsp"><a href="gkgjw.html">Part&nbsp;IX&nbsp;Case Studies</a></p>
<p class="toc level2"><a href="gkaee.html">49.&nbsp;&nbsp;Duke's Tutoring Case Study Example</a></p>
<p class="toc level1 tocsp"><a href="idx-1.html">Index</a></p>
</td>
      <td width="10px">&nbsp;</td>
      <td>
         <div class="header">
             <div class="banner">
                <table width="100%" border="0" cellpadding="5" cellspacing="0">
                   <tbody>
                      <tr>
                         <td valign="bottom"><p class="Banner">The Java EE 6 Tutorial
</p></td>
                         <td align="right"  valign="bottom"><img src="graphics/javalogo.png" alt="Java Coffee Cup logo"></td>
                      </tr>
                   </tbody>
                </table>
             </div>

             <div class="header-links">
	         <a href="./index.html">Home</a> | 
<a href="../information/download.html">Download</a> | 
<a href="./javaeetutorial6.pdf">PDF</a> | 
<a href="../information/faq.html">FAQ</a> | 
<a href="http://download.oracle.com/javaee/feedback.htm">Feedback</a>

             </div>
             <div class="navigation">
                 <a href="gijrb.html"><img src="graphics/leftButton.gif" border="0" alt="Previous" title="Previous"></a>
                 <a href="p1.html"><img src="graphics/upButton.gif" border="0" alt="Contents" title="Contents"></a>
                 <a href="gipvi.html"><img src="graphics/rightButton.gif" border="0" alt="Next" title="Next"></a>
             </div>
         </div>

	 <div class="maincontent">      	 
             

<a name="bnbod"></a><h2>The <tt>cart</tt> Example</h2>
<a name="indexterm-1404"></a><a name="indexterm-1405"></a><p>The <tt>cart</tt> example represents a shopping cart in an online bookstore and uses
a stateful session bean to manage the operations of the shopping cart. The
bean&rsquo;s client can add a book to the cart, remove a book, or
retrieve the cart&rsquo;s contents. To assemble <tt>cart</tt>, you need the following code:</p>


<ul><li><p>Session bean class (<tt>CartBean</tt>)</p>

</li>
<li><p>Remote business interface (<tt>Cart</tt>)</p>

</li></ul>
<p>All session beans require a session bean class. All enterprise beans that permit
remote access must have a remote business interface. To meet the needs of
a specific application, an enterprise bean may also need some helper classes. The
<tt>CartBean</tt> session bean uses two helper classes, <tt>BookException</tt> and <tt>IdVerifier</tt>, which are discussed in
the section <a href="#bnboj">Helper Classes</a>.</p>

<p>The source code for this example is in the <tt></tt><i>tut-install</i><tt>/examples/ejb/cart/</tt> directory.</p>



<a name="bnboe"></a><h3>The Business Interface</h3>
<p><a name="indexterm-1406"></a><a name="indexterm-1407"></a>The <tt>Cart</tt> business interface is a plain Java interface that defines all the
business methods implemented in the bean class. If the bean class implements a
single interface, that interface is assumed to the business interface. The business interface
is a local interface unless it is annotated with the <tt>javax.ejb.Remote</tt> annotation; the <tt>javax.ejb.Local</tt>
annotation is optional in this case.</p>

<p>The bean class may implement more than one interface. In that case, the
business interfaces must either be explicitly annotated <tt>@Local</tt> or <tt>@Remote</tt> or be
specified by decorating the bean class with <tt>@Local</tt> or <tt>@Remote</tt>. However, the
following interfaces are excluded when determining whether the bean class implements more than one
interface:</p>


<ul><li><p><tt>java.io.Serializable</tt></p>

</li>
<li><p><tt>java.io.Externalizable</tt></p>

</li>
<li><p>Any of the interfaces defined by the <tt>javax.ejb</tt> package</p>

</li></ul>
<p>The source code for the <tt>Cart</tt> business interface follows:</p>

<pre>package com.sun.tutorial.javaee.ejb;

import java.util.List;
import javax.ejb.Remote;

@Remote
public interface Cart {
    public void initialize(String person) throws BookException;
    public void initialize(String person, String id)
         throws BookException;
    public void addBook(String title);
    public void removeBook(String title) throws BookException;
    public List&lt;String> getContents();
    public void remove();
}</pre>

<a name="bnbof"></a><h3>Session Bean Class</h3>
<p><a name="indexterm-1408"></a>The session bean class for this example is called <tt>CartBean</tt>. Like any
stateful session bean, the <tt>CartBean</tt> class must meet the following requirements.</p>


<ul><li><p><a name="indexterm-1409"></a>The class is annotated <tt>@Stateful</tt>.</p>

</li>
<li><p>The class implements the business methods defined in the business interface.</p>

</li></ul>
<p>Stateful session beans also may</p>


<ul><li><p>Implement the business interface, a plain Java interface. It is good practice to implement the bean&rsquo;s business interface.</p>

</li>
<li><p><a name="indexterm-1410"></a><a name="indexterm-1411"></a><a name="indexterm-1412"></a><a name="indexterm-1413"></a>Implement any optional lifecycle callback methods, annotated <tt>@PostConstruct</tt>, <tt>@PreDestroy</tt>, <tt>@PostActivate</tt>, and <tt>@PrePassivate</tt>.</p>

</li>
<li><p><a name="indexterm-1414"></a>Implement any optional business methods annotated <tt>@Remove</tt>.</p>

</li></ul>
<p>The source code for the <tt>CartBean</tt> class follows:</p>

<pre>package com.sun.tutorial.javaee.ejb;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;

@Stateful
public class CartBean implements Cart {
    String customerName;
    String customerId;
    List&lt;String> contents;

    public void initialize(String person) throws BookException {
        if (person == null) {
            throw new BookException("Null person not allowed.");
        } else {
            customerName = person;
        }

        customerId = "0";
        contents = new ArrayList&lt;String>();
    }

    public void initialize(String person, String id)
                 throws BookException {
        if (person == null) {
            throw new BookException("Null person not allowed.");
        } else {

            customerName = person;
        }

        IdVerifier idChecker = new IdVerifier();

        if (idChecker.validate(id)) {
            customerId = id;
        } else {
            throw new BookException("Invalid id: " + id);
        }

        contents = new ArrayList&lt;String>();
    }

    public void addBook(String title) {
        contents.add(title);
    }

    public void removeBook(String title) throws BookException {
        boolean result = contents.remove(title);
        if (result == false) {
            throw new BookException(title + " not in cart.");
        }
    }

    public List&lt;String> getContents() {
        return contents;
    }

    @Remove
    public void remove() {
        contents = null;
    }
}</pre>

<a name="bnbog"></a><h4>Lifecycle Callback Methods</h4>
<a name="indexterm-1415"></a><a name="indexterm-1416"></a><a name="indexterm-1417"></a><a name="indexterm-1418"></a><p>A method in the bean class may be declared as a lifecycle
callback method by annotating the method with the following annotations:</p>


<ul><li><p><tt>javax.annotation.PostConstruct</tt>: Methods annotated with <tt>@PostConstruct</tt> are invoked by the container on newly constructed bean instances after all dependency injection has completed and before the first business method is invoked on the enterprise bean.</p>

</li>
<li><p><tt>javax.annotation.PreDestroy</tt>: Methods annotated with <tt>@PreDestroy</tt> are invoked after any method annotated <tt>@Remove</tt> has completed and before the container removes the enterprise bean instance.</p>

</li>
<li><p><tt>javax.ejb.PostActivate</tt>: Methods annotated with <tt>@PostActivate</tt> are invoked by the container after the container moves the bean from secondary storage to active status.</p>

</li>
<li><p><tt>javax.ejb.PrePassivate</tt>: Methods annotated with <tt>@PrePassivate</tt> are invoked by the container before it passivates the enterprise bean, meaning that the container temporarily removes the bean from the environment and saves it to secondary storage.</p>

</li></ul>
<p>Lifecycle callback methods must return <tt>void</tt> and have no parameters.</p>



<a name="bnboh"></a><h4>Business Methods</h4>
<p><a name="indexterm-1419"></a>The primary purpose of a session bean is to run business tasks for
the client. The client invokes business methods on the object reference it gets
from dependency injection or JNDI lookup. From the client&rsquo;s perspective, the business methods
appear to run locally, although they run remotely in the session bean. The
following code snippet shows how the <tt>CartClient</tt> program invokes the business methods:</p>

<pre>cart.create("Duke DeEarl", "123");
...
cart.addBook("Bel Canto");
 ...
List&lt;String> bookList = cart.getContents();
...
cart.removeBook("Gravity&rsquo;s Rainbow");</pre><p>The <tt>CartBean</tt> class implements the business methods in the following code:</p>

<pre>public void addBook(String title) {
   contents.addElement(title);
}

public void removeBook(String title) throws BookException {
   boolean result = contents.remove(title);
   if (result == false) {
      throw new BookException(title + "not in cart.");
   }
}

public List&lt;String> getContents() {
   return contents;
}</pre><p><a name="indexterm-1420"></a>The signature of a business method must conform to these rules.</p>


<ul><li><p>The method name must not begin with <tt>ejb</tt>, to avoid conflicts with callback methods defined by the EJB architecture. For example, you cannot call a business method <tt>ejbCreate</tt> or <tt>ejbActivate</tt>.</p>

</li>
<li><p>The access control modifier must be <tt>public</tt>.</p>

</li>
<li><p>If the bean allows remote access through a remote business interface, the arguments and return types must be legal types for the Java Remote Method Invocation (RMI) API.</p>

</li>
<li><p><a name="indexterm-1421"></a>If the bean is a web service endpoint, the arguments and return types for the methods annotated <tt>@WebMethod</tt> must be legal types for JAX-WS.</p>

</li>
<li><p>The modifier must not be <tt>static</tt> or <tt>final</tt>.</p>

</li></ul>
<p>The <tt>throws</tt> clause can include exceptions that you define for your application. The
<tt>removeBook</tt> method, for example, throws a <tt>BookException</tt> if the book is not in the
cart.</p>

<p><a name="indexterm-1422"></a><a name="indexterm-1423"></a><a name="indexterm-1424"></a>To indicate a system-level problem, such as the inability to connect to a
database, a business method should throw a <tt>javax.ejb.EJBException</tt>. The container will not
wrap application exceptions, such as <tt>BookException</tt>. Because <tt>EJBException</tt> is a subclass of <tt>RuntimeException</tt>,
you do not need to include it in the <tt>throws</tt> clause of the
business method.</p>



<a name="bnboi"></a><h3>The <tt>@Remove</tt> Method</h3>
<a name="indexterm-1425"></a><p>Business methods annotated with <tt>javax.ejb.Remove</tt> in the stateful session bean class can be
invoked by enterprise bean clients to remove the bean instance. The container will
remove the enterprise bean after a <tt>@Remove</tt> method completes, either normally or abnormally.</p>

<p>In <tt>CartBean</tt>, the <tt>remove</tt> method is a <tt>@Remove</tt> method:</p>

<pre>@Remove
public void remove() {
    contents = null;
}</pre>

<a name="bnboj"></a><h3>Helper Classes</h3>
<p><a name="indexterm-1426"></a>The <tt>CartBean</tt> session bean has two helper classes: <tt>BookException</tt> and <tt>IdVerifier</tt>. The <tt>BookException</tt>
is thrown by the <tt>removeBook</tt> method, and the <tt>IdVerifier</tt> validates the <tt>customerId</tt> in one
of the <tt>create</tt> methods. Helper classes may reside in an EJB JAR file
that contains the enterprise bean class, a WAR file if the enterprise bean
is packaged within a WAR, or in an EAR that contains an EJB
JAR or a WAR file that contains an enterprise bean.</p>



<a name="bnbok"></a><h3>Building, Packaging, Deploying, and Running the <tt>cart</tt> Example</h3>
<p>Now you are ready to compile the remote interface (<tt>Cart.java</tt>), the home interface
(<tt>CartHome.java</tt>), the enterprise bean class (<tt>CartBean.java</tt>), the client class (<tt>CartClient.java</tt>), and the helper
classes (<tt>BookException.java</tt> and <tt>IdVerifier.java</tt>). Follow these steps.</p>

<p>You can build, package, deploy, and run the <tt>cart</tt> application using either NetBeans
IDE or the Ant tool.</p>



<a name="bnbol"></a><h4>To Build, Package, Deploy, and Run the <tt>cart</tt> Example Using NetBeans IDE</h4>
<ol>
<li><b>From the File menu, choose Open Project.</b></li>
<li><b>In the Open Project dialog, navigate to:</b><pre><tt></tt><i>tut-install</i><tt>/examples/ejb/</tt></pre></li>
<li><b>Select the <tt>cart</tt> folder.</b></li>
<li><b>Select the Open as Main Project and Open Required Projects check boxes.</b></li>
<li><b>Click Open Project.</b></li>
<li><b>In the Projects tab, right-click the <tt>cart</tt> project and select Deploy.</b><p>This builds and packages the application into <tt>cart.ear</tt>, located in <tt></tt><i>tut-install</i><tt>/examples/ejb/cart/dist/</tt>, and deploys
this EAR file to your GlassFish Server instance.</p></li>
<li><b>From the Run menu, choose Run Main Project.</b><p>You will see the output of the <tt>cart</tt> application client in the Output pane:</p><pre>...
Retrieving book title from cart: Infinite Jest
Retrieving book title from cart: Bel Canto
Retrieving book title from cart: Kafka on the Shore
Removing "Gravity&rsquo;s Rainbow" from cart.
Caught a BookException: "Gravity&rsquo;s Rainbow" not in cart.
Java Result: 1
run-cart-app-client:
run-nb:
BUILD SUCCESSFUL (total time: 14 seconds)</pre></li></ol>

<a name="bnbon"></a><h4>To Build, Package, Deploy, and Run the <tt>cart</tt> Example Using Ant</h4>
<ol>
<li><b>In a terminal window, go to:</b><pre><i>tut-install</i>/examples/ejb/cart/</pre></li>
<li><b>Type the following command:</b><pre><tt><b>ant</b></tt></pre><p>This command calls the <tt>default</tt> target, which builds and packages the application into
an EAR file, <tt>cart.ear</tt>, located in the <tt>dist</tt> directory.</p></li>
<li><b>Type the following command:</b><pre><tt><b>ant deploy</b></tt></pre><p>The <tt>cart.ear</tt> file is deployed to the GlassFish Server.</p></li>
<li><b>Type the following command:</b><pre><tt><b>ant run</b></tt></pre><p>This task retrieves the application client JAR, <tt>cartClient.jar</tt>, and runs the application client.
The client JAR, <tt>cartClient.jar</tt>, contains the application client class, the helper class <tt>BookException</tt>, and
the <tt>Cart</tt> business interface.</p><p>This task is equivalent to running the following command:</p><pre><tt><b>appclient -client cartClient.jar</b></tt></pre><p>When you run the client, the application client container injects any component references
declared in the application client class, in this case the reference to the
<tt>Cart</tt> enterprise bean.</p></li></ol>

<a name="bnbop"></a><h4>The <tt>all</tt> Task</h4>
<p>As a convenience, the <tt>all</tt> task will build, package, deploy, and run the
application. To do this, enter the following command:</p>

<pre><tt><b>ant all</b></tt></pre>
         </div>
         <div class="navigation">
             <a href="gijrb.html"><img src="graphics/leftButton.gif" border="0" alt="Previous" title="Previous"></a>
             <a href="p1.html"><img src="graphics/upButton.gif" border="0" alt="Contents" title="Contents"></a>
             <a href="gipvi.html"><img src="graphics/rightButton.gif" border="0" alt="Next" title="Next"></a>
         </div>

         <div class="copyright">
      	    <p>Copyright &copy; 2011, Oracle and/or its affiliates. All rights reserved. <a href="docinfo.html">Legal Notices</a></p>
      	 </div>

      </td>
   </tr>
</tbody>
</table>
</body>
</html>

